package com.example.test.utils;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.example.test.demin.Bill;
import com.example.test.demin.Card;
import com.example.test.demin.TingcheChang;
import com.example.test.demin.User;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DbDaoUtils {
	private Context ct;

	public DbDaoUtils(Context ct) {
		dbHelper = new DbHelper(ct, "tingche.db", null, 1);
		this.ct = ct;
	}

	private SQLiteDatabase db = null;
	private DbHelper dbHelper;

	public long insertUser(String username, String password, String phone,
			double money) {
		db = dbHelper.getWritableDatabase();

		ContentValues cv = new ContentValues();
		cv.put("username", username);
		cv.put("password", password);
		cv.put("phone", phone);
		cv.put("money", money);
		long res = db.insert("user", null, cv);
		db.close();
		return res;
	}

	public long insertBill(String tingname, String carnumber, 
			double money) {
		db = dbHelper.getWritableDatabase();
		Date   curDate   =   new   Date(System.currentTimeMillis());//获取当前时间 
		SimpleDateFormat sDateFormat  =  new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");    
        String billnumber="6224835463"+curDate.getMinutes();
		ContentValues cv = new ContentValues();	
		cv.put("billnumber", billnumber);
		cv.put("tingname", tingname);
		cv.put("carnumber", carnumber);
		cv.put("time", curDate.toLocaleString());
		cv.put("cost", money);

		long res = db.insert("billlist", null, cv);
		db.close();
		return res;
	}
	public long insertCard(String bankname, String cardnumber, double money) {
		db = dbHelper.getWritableDatabase();

		ContentValues cv = new ContentValues();
		cv.put("bankname", bankname);
		cv.put("cardnumber", cardnumber);
		cv.put("money", money);
		long res = db.insert("card", null, cv);
		db.close();
		return res;
	}
	public long insertCarnumber(String carnumber) {
		db = dbHelper.getWritableDatabase();
		ContentValues cv = new ContentValues();		
		cv.put("carnumber", carnumber);
		long res = db.insert("carnumber", null, cv);
		db.close();
		return res;
	}

	/***
	 * ��ѯ�Ƿ����
	 * 
	 * @param number
	 * @return
	 */
	public boolean findUser(String username, String password) {
		boolean result = false;
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"select * from user where username=? and password=? ",
				new String[] { username, password });
		if (cursor.moveToNext()) {
			result = true;
		}
		cursor.close();
		db.close();
		return result;
	}

	public User findUserInfo(String username) {
		User user = null;
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from user where username=? ",
				new String[] { username });
		if (cursor.moveToNext()) {
			user = new User();
			int id = cursor.getInt(0);
			String password = cursor.getString(2);
			String phone = cursor.getString(3);
			double money = cursor.getDouble(4);
			user.setId(id);
			user.setMoney(money);
			user.setPassword(password);
			user.setUsername(username);
			user.setPhone(phone);
		}
		cursor.close();
		db.close();
		return user;
	}
	public Bill findBillInfo(int _id) {
		Bill bill = null;
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from billlist where _id=? ",
				new String[] { String.valueOf(_id)});
		if (cursor.moveToNext()) {
			bill = new Bill();
			int id = cursor.getInt(0);
			String billnumber=cursor.getString(1);
			String tingname = cursor.getString(2);
			String carnumber = cursor.getString(3);
			String time = cursor.getString(4);
			double money = cursor.getDouble(5);
			bill.set_id(_id);
			bill.setCost(money);
			bill.setTingname(tingname);
		    bill.setBillnumber(billnumber);
		    bill.setCarnumber(carnumber);
		    bill.setTime(time);
		}
		cursor.close();
		db.close();
		return bill;
	}
	public List<Bill> findALLBillInfo() {
		List<Bill> list = new ArrayList<Bill>();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from billlist",null);
		while (cursor.moveToNext()) {
			Bill bill = new Bill();
			int id = cursor.getInt(0);
			String billnumber=cursor.getString(1);
			String tingname = cursor.getString(2);
			String carnumber = cursor.getString(3);
			String time = cursor.getString(4);
			double money = cursor.getDouble(5);
			bill.set_id(id);
			bill.setCost(money);
			bill.setTingname(tingname);
		    bill.setBillnumber(billnumber);
		    bill.setCarnumber(carnumber);
		    bill.setTime(time);
		    list.add(bill);
		}
		cursor.close();
		db.close();
		return list;
	}
	public Card findCardInfo(String cardnumber) {
		Card card = null;
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from card where cardnumber=? ",
				new String[] { cardnumber });
		if (cursor.moveToNext()) {
			card = new Card();
			
			String bankname = cursor.getString(0);
			cardnumber = cursor.getString(1);
			double money = cursor.getDouble(2);
			card.setMoney(money);
			card.setBankname(bankname);
			card.setCardnumber(cardnumber);
			
		}
		cursor.close();
		db.close();
		return card;
	}


	public TingcheChang findTingcheInfobyTnumber(String tnumber) {
		TingcheChang tingcheChang = null;
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"select * from tingchechang where tnumber=? ",
				new String[] { tnumber });
		if (cursor.moveToNext()) {
			tingcheChang = new TingcheChang();
			int id = cursor.getInt(0);
			String name = cursor.getString(1);
			String idname = cursor.getString(2);
			String carnumber = cursor.getString(3);
			double money = cursor.getDouble(5);
			tingcheChang.set_id(id);
			tingcheChang.setMoney(money);
			tingcheChang.setCarnumber(carnumber);
			tingcheChang.setName(name);
			tingcheChang.setIdname(idname);
			tingcheChang.setTnumber(tnumber);
		}
		cursor.close();
		db.close();
		return tingcheChang;
	}

	public TingcheChang findTingcheInfobyCarnumber(String tingchecahng, String carnumber) {
		TingcheChang tingcheChang = null;
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"select * from tingchechang where carnumber=? and name=? ",
				new String[] { carnumber,tingchecahng });
		if (cursor.moveToNext()) {
			tingcheChang = new TingcheChang();
			int id = cursor.getInt(0);
			String name = cursor.getString(1);
			String idname = cursor.getString(2);
			String tnumber = cursor.getString(4);
			double money = cursor.getDouble(5);
			tingcheChang.set_id(id);
			tingcheChang.setMoney(money);
			tingcheChang.setCarnumber(carnumber);
			tingcheChang.setName(name);
			tingcheChang.setIdname(idname);
			tingcheChang.setTnumber(tnumber);
		}
		cursor.close();
		db.close();
		return tingcheChang;
	}

	public void updateMoney(String username, double money) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("money", money);
		db.update("user", values, "username=?", new String[] { username });
		db.close();
	}
	public void updateCardMoney(String cardnumber, double money) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("money", money);
		db.update("card", values, "cardnumber=?", new String[] { cardnumber });
		db.close();
	}


	public User selectAllUser(String username) {
		db = dbHelper.getReadableDatabase();
		User user = new User();

		Cursor cursor = db.query("user", null, "username=? ",
				new String[] { username, }, null, null, null);
		if (cursor.moveToNext()) {
			int id = cursor.getInt(0);
			String phone = cursor.getString(3);
			double money = cursor.getDouble(4);
			user.setId(id);
			user.setMoney(money);
			user.setPhone(phone);
			user.setUsername(username);
		}
		return user;

	}

	public List<User> findAll() {
		List<User> list = new ArrayList<User>();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"select _id,username,password,phone,money from user", null);
		while (cursor.moveToNext()) {
			User info = new User();
			int id = cursor.getInt(0);
			String username = cursor.getString(1);
			String password = cursor.getString(2);
			String phone = cursor.getString(3);
			double money = cursor.getDouble(4);
			info.setId(id);
			info.setUsername(username);
			info.setPassword(password);
			info.setMoney(money);
			info.setPhone(phone);
			list.add(info);
		}
		cursor.close();
		db.close();
		return list;

	}

	public List<Card> findAllCard() {
		List<Card> list = new ArrayList<Card>();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"select * from card", null);
		while (cursor.moveToNext()) {
			Card info = new Card();
			String bankname = cursor.getString(0);
			String cardnumber = cursor.getString(1);
			double money = cursor.getDouble(2);
			info.setBankname(bankname);
			info.setCardnumber(cardnumber);
			info.setMoney(money);
			list.add(info);
		}
		cursor.close();
		db.close();
		return list;

	}
	
	public List<String> findAllCarNumber() {
		List<String> list = new ArrayList<String>();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"select * from carnumber", null);
		while (cursor.moveToNext()) {			
			String carnumber = cursor.getString(0);		
			list.add(carnumber);
		}
		cursor.close();
		db.close();
		return list;

	}
}
